package com.yunkuhui.www.demo.configuration;

import com.yunkuhui.www.demo.filter.DefaultFilter;
import com.yunkuhui.www.demo.filter.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

@Configuration
@SuppressWarnings("unchecked")
public class FilterConfiguration {

	@Resource
	private DefaultFilter defaultFilter;

	@Resource
	private MyFilter myFilter;

	@Bean
	public FilterRegistrationBean defaultFilterRegistrationBean() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(defaultFilter);
		Set<String> set = new HashSet<>();
		set.add("/filter/1"); // 不可以写 /demo/filter/1
		set.add("/filter/2");
		registration.setUrlPatterns(set);
		registration.setName("defaultFilter");
		registration.setOrder(0);
		return registration;
	}

	@Bean
	public FilterRegistrationBean myFilterRegistrationBean() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(myFilter);
		Set<String> set = new HashSet<>();
		set.add("/*");
		registration.setUrlPatterns(set);
		registration.setName("myFilter");
		registration.setOrder(1);
		return registration;
	}
}
